#include "moverdown.h"

MoverDown::MoverDown(QWidget *parent) : QWidget(parent)
{

}

MoverDown::MoverDown(const QString &Name, const QJsonObject &functionDetails, QWidget *parent)
    : QWidget(parent)
{
    QGroupBox *groupBox = new QGroupBox("下mover控制", this);
    groupBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    createMoverDownControl(groupBox, functionDetails);

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(groupBox);
    layout->setContentsMargins(0, 0, 0, 0);
    qDebug() << "测试是否进入整体控制";
    setLayout(layout);
}

void MoverDown::createMoverDownControl(QGroupBox *groupBox, const QJsonObject &functionDetails)
{
    qDebug() << "functionDetails :" << functionDetails;
    QVBoxLayout *mainLayout = new QVBoxLayout();
    groupBox->setLayout(mainLayout);

    // 遍历 functionDetails，动态创建控件
    for (auto i = functionDetails.begin(); i != functionDetails.end(); ++i) {
        // 获取设置项名称
        QString settingName = i.key();

        // 获取每个设置项的值（QJsonObject）
        QJsonObject settingDetails = i.value().toObject();

        // 创建一个新的 QWidget 用于该设置项的控件
        QWidget *settingWidget = new QWidget(groupBox);
        QHBoxLayout *settingLayout = new QHBoxLayout(settingWidget);

        // 创建标签，并根据设置项的名称来决定显示内容
        QLabel *label = new QLabel(settingWidget);

        if (settingName == "tonnage") {
            label->setText("液压站吨数(T):");
        } else if (settingName == "pressure") {
            label->setText("液压站压力(0.1Bar):");
        } else if(settingName == "speed"){
            label->setText("液压站转速(rpm):");
        }

        // 设置固定宽度，并使标签内容居中
        label->setFixedWidth(150);  // 固定宽度，确保对齐
        label->setAlignment(Qt::AlignCenter);  // 居中显示

        // 获取 JSON 中的默认值
        int defaultValue = settingDetails["DefaultValue"].toInt();
        QLineEdit *input = new QLineEdit(settingWidget);
        input->setText(QString::number(defaultValue));

        // 创建设置按钮
        QPushButton *button = new QPushButton("设置", settingWidget);

        // 创建弹簧控件（QSpacerItem）
        QSpacerItem *spacer1 = new QSpacerItem(20, 40, QSizePolicy::Fixed, QSizePolicy::Expanding);  // 标签和输入框之间
        QSpacerItem *spacer2 = new QSpacerItem(20, 40, QSizePolicy::Fixed, QSizePolicy::Expanding);  // 输入框和按钮之间
        QSpacerItem *spacer3 = new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Fixed);  // 左侧
        QSpacerItem *spacer4 = new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Fixed);  // 右侧

        // 将标签、输入框、按钮和弹簧控件添加到该类型的布局中
        settingLayout->addItem(spacer3);   // 左侧弹簧
        settingLayout->addWidget(label);   // 标签
        settingLayout->addItem(spacer1);   // 标签和输入框之间的弹簧
        settingLayout->addWidget(input);   // 输入框
        settingLayout->addItem(spacer2);   // 输入框和按钮之间的弹簧
        settingLayout->addWidget(button);  // 按钮
        settingLayout->addItem(spacer4);   // 右侧弹簧

        // 将该控件添加到主布局中
        mainLayout->addWidget(settingWidget);

        // 连接按钮点击信号到槽函数
        connect(button, &QPushButton::clicked, this, &MoverDown::onButtonClicked);
    }

}

void MoverDown::onButtonClicked()
{
    QPushButton *button = qobject_cast<QPushButton *>(sender());
    if (!button)
        return;

    // 获取父布局中的 QLineEdit 和 QLabel 控件
    QWidget *parentWidget = button->parentWidget();
    QLineEdit *input = parentWidget->findChild<QLineEdit *>();
    QLabel *label = parentWidget->findChild<QLabel *>();

    if (input && label) {
        QString labelText = label->text();
        int value = input->text().toInt();

        qDebug() << "标签内容：" << labelText << ", 输入内容：" << input->text();
        qDebug() << "设置值为：" << value;

//        // 根据按钮的文本来判断执行的逻辑
//        if (button->text() == "设置") {
//            qDebug() << "执行设置操作，值为：" << value;
//            // 在这里添加设置操作的具体逻辑
//        }
    }
}
